package com.ajaxjs.user.password;

import java.util.Random;

public abstract class Password {
	/**
	 * AES 密钥，前后端约定好的密钥
	 */
	private String AesKey;

	/**
	 * 密码里加一些“Salt值”
	 */
	private String salt;

	/**
	 * 盐值长度，这个 int 很重要，千万不能让别人知道！
	 */
	private int saltSize = 8;

	public String getAesKey() {
		return AesKey;
	}

	public void setAesKey(String aesKey) {
		AesKey = aesKey;
	}

	public String getSalt() {
		return salt;
	}

	public void setSalt(String salt) {
		this.salt = salt;
	}

	private static final String text = "0123456789abcdefghijklmnopqrstuvwxyz";

	/**
	 * 生成随机密码。可以控制生成的密码长度， 密码由数字和字母组成。
	 * 
	 * @param length
	 *            字符串长度
	 * @return 随机密码
	 */
	public synchronized static String passwordGenerator(int length) {
		StringBuffer sb = new StringBuffer();
		Random random = new Random();

		for (int i = 0; i < length; i++)
			sb.append(text.charAt(random.nextInt(text.length())));

		return sb.toString();
	}

	public String passwordGenerator() {
		return passwordGenerator(getSaltSize());
	}

	public int getSaltSize() {
		return saltSize;
	}

	public void setSaltSize(int saltSize) {
		this.saltSize = saltSize;
	}
}
